package java6699.lesson08;


import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;

import java.util.Comparator;

/**
 * @program: java_zzx
 * @description:
 * @author: zzx
 * @create: 2022-10-26 22:02
 **/

public class ListSort6699Controller {
    @FXML
    public ToggleGroup tg;
    @FXML
    public ListView<Course6699> lvCourse;
    @FXML
    private RadioButton rb1;


    private ObservableList<Course6699> clist;

    @FXML
    void initialize() {
        clist = lvCourse.getItems();
        //添加课程
        clist.add(new Course6699("A001", "C程序设计基础", "1上", 4, 80));
        clist.add(new Course6699("A002", "面向对象程序设计", "2上", 3, 78));
        clist.add(new Course6699("A003", "数据库原理", "2上", 3, 78));
        clist.add(new Course6699("B001", "操作系统", "2下", 3, 90));
        clist.add(new Course6699("B002", "Python程序基础", "1下", 2, 85));
        clist.add(new Course6699("B003", "网络基础", "3上", 3, 75));
        clist.add(new Course6699("B003", "网络基础", "3上", 3, 75));
        clist.add(new Course6699("C001", "Java程序高级开发", "3上", 2, 83));
        clist.add(new Course6699("C002", "软件工程与UML", "3下", 3, 75));
        clist.add(new Course6699("0001", "大学英语", "1上", 3, 80));
        clist.add(new Course6699("0002", "高等数学", "1下", 2, 85));
        //注册监听
        tg.selectedToggleProperty().addListener(this::sorter);
        tg.selectToggle(rb1);


    }

    @FXML
    void sorter(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
        //转为Radiod
        RadioButton rb = (RadioButton) newValue;
        switch (rb.getText()) {
            case "按学期升序、成绩降序":
                clist.sort(Comparator.naturalOrder());
                break;
            case "按成绩降序":
                clist.sort(Comparator.comparingInt(Course6699::getScore).reversed());
                break;
            case "按学分升序":
                clist.sort(Comparator.comparingInt(Course6699::getCredit));
                break;
            case "按学期升序":
                clist.sort(Comparator.comparing(Course6699::getTerm));
                break;
            case "按学期升序、学分降序":
                clist.sort(Comparator.comparing(Course6699::getTerm).reversed().thenComparing(Course6699::getCredit).reversed());
                break;
            default:
        }

    }

}
